'use strict';

angular.module('oaApp')
  .controller('TimelineCtrl', function ($scope, $http, socket, $timeout) {
    $scope.feeds = [];
    $scope.feedsLoading = false;

    $timeout(function () {
      var now = new Date();
      $http.get('/api/timeline?created='+now)
        .success(function (res) {
          $scope.feeds = res;
          $scope.feedsLoading = true;
          socket.syncUpdates('timeline', $scope.feeds);
          console.log('成功获取时间轴');
        })
        .error(function (err) {
          console.log('获取时间轴错误');
        });
    }, 500);

    $scope.loadMore = function () {
      var url = '/api/timeline?created='+$scope.feeds[$scope.feeds.length-1].created_at;
      console.log(url);
      $http.get(url)
        .success(function (res) {
          angular.forEach(res,function(item) {
            $scope.feeds.push(item);
          });
          console.log(res);
          console.log('成功获取时间轴');
        })
        .error(function (err) {
          console.log('获取时间轴错误');
        });
    };

    $scope.$on('$destroy', function () {
      socket.unsyncUpdates('timeline');
    });
  });
